package com.kangkang.reflection.classEx;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 描述:
 * 日期: 2022-03-18 <br>
 *
 * @author: 旺旺老师<br>
 * @version: 1.0<br>
 */
public class ReflectionTool {
    public static void printClassMsg(Class cls) {
        printFilds(cls);
        printMethod(cls);
        printPercent(cls);
    }
    public static void printPercent(Class cls) {
        //1 得到父类
        Class superCls = cls.getSuperclass();
        System.out.println("superClass : " + superCls.getName());

        //2 得到接口
        Class[] supserInterfaces = cls.getInterfaces();
        if (supserInterfaces.length != 0 ) {
            System.out.println(cls.getName() + "实现的接口有:");
            for (Class superInterface : supserInterfaces) {
                System.out.println(superInterface);
            }
        } else {
            System.out.println(cls.getName() + "没有实现接口");
        }
    }
    public static void printMethod(Class cls) {
        System.out.println("方法有:");
        //Class<Student> cls = Student.class;
        //getMethods(); 得到所有 包括父类
        //getDeclaredMethods(); 只是本类中定义的
        Method[] ms = cls.getDeclaredMethods();
        for (Method method : ms) {
            String modifierName = "";
            switch (method.getModifiers()) {
                case 1:
                    modifierName = "public";
                    break;
                case 2:
                    modifierName = "private";
                    break;
                case 4:
                    modifierName = "protected";
                    break;
            }
            System.out.println(modifierName + "  "
                    + method.getReturnType() + "  "
                    + method.getName() + "  "
                    + Arrays.toString(method.getParameterTypes()));
        }
    }
    public static void printFilds(Class cls) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            String modifierName = "";
            switch (field.getModifiers()) {
                case 1:
                    modifierName = "public";
                    break;
                case 2:
                    modifierName = "private";
                    break;
                case 4:
                    modifierName = "protected";
                    break;
            }
            System.out.println(modifierName + " " + field.getType() + " " + field.getName() + ";");
        }

    }
}
